package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.base.MyBaseAdapter;
import com.kkd.kkdapp.http.manager.HttpCallBack;

import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.LogUtil;


/**
 * 还款详情
 * 
 * @author mty
 * 
 */
public class RepaymentDetailActivity extends BaseActivity implements
		OnClickListener {

	private RelativeLayout td_top_img; // 返回键
	ListView mListView;
	ListAdapter mAdapter;
	private JSONArray data;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				try {
					if (data != null && data.length() > 0) {
						List<JSONObject> dataList = new ArrayList<JSONObject>();
						for (int i = 0; i < data.length(); i++) {
							dataList.add(data.getJSONObject(i));
						}
						mAdapter.appendData(dataList, true);
						mAdapter.notifyDataSetChanged();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				break;
			case 2:
				String desc = (String) msg.obj;
				showToast(desc);
				break;
			default:
				break;
			}
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_repayment_detail);
		initView();
		setListener();
	}

	public void initView() {
		td_top_img = (RelativeLayout) this.findViewById(R.id.td_top_img);
		mListView = (ListView) this.findViewById(R.id.repayment_detail_list);
		mAdapter = new ListAdapter(this);
		mListView.setAdapter(mAdapter);
		String poiId = getIntent().getStringExtra("poiId");
		requestData(poiId);
	}

	public void setListener() {
		td_top_img.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.td_top_img:
			finish();
			break;
		}

	}

	private class ListAdapter extends MyBaseAdapter<JSONObject> {

		LayoutInflater mInflater;

		public ListAdapter(Context context) {
			super(context);
			mInflater = LayoutInflater.from(context);
		}

		@Override
		public View getMyView(int position, View convertView, ViewGroup parent) {
			ViewHodler mViewHodler;
			if (convertView == null) {
				mViewHodler = new ViewHodler();
				convertView = mInflater.inflate(R.layout.repayment_list_item,
						null);
				mViewHodler.repayTime = (TextView) convertView
						.findViewById(R.id.repay_time);
				mViewHodler.repayCount = (TextView) convertView
						.findViewById(R.id.repay_count);
				mViewHodler.value1 = (TextView) convertView
						.findViewById(R.id.value1);
				mViewHodler.value2 = (TextView) convertView
						.findViewById(R.id.value2);
				mViewHodler.repayment_img = (ImageView) convertView
						.findViewById(R.id.repayment_img);
				convertView.setTag(mViewHodler);

			} else {
				mViewHodler = (ViewHodler) convertView.getTag();
			}
			JSONObject data = myList.get(position);
			try {
				if (data.getInt("payment_status") == 0) {
					mViewHodler.repayment_img.setVisibility(View.INVISIBLE);
				} else {
					mViewHodler.repayment_img.setVisibility(View.VISIBLE);
				}
				DecimalFormat df = new DecimalFormat("0.00");
				LogUtil.e("date", data.getString("date"));
				mViewHodler.repayTime.setText((data.getString("date")));
				mViewHodler.repayCount.setText(data.getString("bill_num"));
				String total = df.format(Double.parseDouble(data
						.getString("total")));
				String income = df.format(Double.parseDouble(data
						.getString("income")));
				mViewHodler.value1.setText(total);
				mViewHodler.value2.setText(income);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHodler {
			TextView repayTime;
			TextView repayCount;
			TextView value1;
			TextView value2;
			ImageView repayment_img;
		}
	}

	/**
	 * 请求接口
	 */
	public void requestData(String poiId) {

		LogUtil.e("poiId", poiId);

		KkdHttpMagager.getInstance().getRepaymentDetailManager(poiId,
				String.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						String responseString = responseBody.toString();
						try {
							data = new JSONArray(responseString);
							mHandler.sendEmptyMessage(1);
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

					@Override
					public void onFailure(int statusCode, String desc) {

						LogUtil.e("desc", desc);
						Message msg = new Message();
						msg.what = 2;
						msg.obj = desc;
						mHandler.sendMessage(msg);
					}
				});
	}

}
